//
// 嗨~ o(*￣▽￣*)ブ
// Created by Mokou on 2020/9/22.
//

#import "Util.h"
#import "ElecDevice.h"
#import "GlobalData.h"


@implementation ElecDevice

- (instancetype)init:(NSDictionary *)_data {
    if (self = super.init) {
        if ([Util notNull:_data k:@"box_sn"])
            self.box_sn = _data[@"box_sn"];

        if ([Util notNull:_data k:@"user_id"])
            self.user_id = _data[@"user_id"];

        if ([Util notNull:_data k:@"device_name"])
            self.device_name = _data[@"device_name"];

        if ([Util notNull:_data k:@"mac"])
            self.mac = _data[@"mac"];

        if ([Util notNull:_data k:@"soft_version"])
            self.soft_version = _data[@"soft_version"];

        if ([Util notNull:_data k:@"net_type"])
            self.net_type = [_data[@"net_type"] intValue];

        if ([Util notNull:_data k:@"ap_user_name"])
            self.ap_user_name = _data[@"ap_user_name"];

        if ([Util notNull:_data k:@"ap_passwd"])
            self.ap_passwd = _data[@"ap_passwd"];

    }
    return self;
}

- (NSDictionary *)getDict {
    NSMutableDictionary *res = @{}.mutableCopy;

    if(self.box_sn)
        res[@"box_sn"] = self.box_sn;

    if(self.user_id)
        res[@"user_id"] = self.user_id;

    if(self.device_name)
        res[@"device_name"] = self.device_name;

    if(self.mac)
        res[@"mac"] = self.mac;

    if(self.soft_version)
        res[@"soft_version"] = self.soft_version;

    if(self.net_type)
        res[@"net_type"] = @(self.net_type);

    if(self.ap_user_name)
        res[@"ap_user_name"] = self.ap_user_name;

    if(self.ap_passwd)
        res[@"ap_passwd"] = self.ap_passwd;

    return res;
}

+ (BOOL)store:(NSDictionary *)_data{
    BOOL callBackRes = NO;

    if (![Util notNull:_data k:@"data"]) return callBackRes;
    NSDictionary *data = _data[@"data"];

    if (![Util notNull:data k:@"user_id"]) return callBackRes;

    NSString *userId = data[@"user_id"];
    NSMutableArray *device = ((NSArray *)data[@"device"]).mutableCopy;

    for(int i=0;i<device.count;i++) {
        NSMutableDictionary *deviceData = ((NSDictionary *)device[i]).mutableCopy;
        deviceData[@"user_id"] = userId;
        ElecDevice *d = [[ElecDevice alloc] init:deviceData];
        device[i] = d;
    }

    Database *db = [GlobalData shareInstance].db;
    [db device_store:device];
    callBackRes = YES;

    return callBackRes;
}

+ (BOOL)delete:(NSDictionary *)_data{
    BOOL callBackRes = NO;

    if (![Util notNull:_data k:@"data"]) return callBackRes;
    NSDictionary *data = _data[@"data"];

    NSString *userId = nil;
    if ([Util notNull:data k:@"user_id"]) userId = data[@"user_id"];
    NSString *boxSn = nil;
    if ([Util notNull:data k:@"box_sn"]) boxSn = data[@"box_sn"];

    Database *db = [GlobalData shareInstance].db;
    [db device_delete:userId boxSn:boxSn];
    return YES;
}

+ (void)clear:(NSDictionary *)_data{
    if (![Util notNull:_data k:@"data"]) return;
    NSDictionary *data = _data[@"data"];

    NSString *userId = nil;
    if ([Util notNull:data k:@"user_id"]) userId = data[@"user_id"];

    Database *db = [GlobalData shareInstance].db;
    [db device_clear:userId];
}

+ (NSDictionary *)query:(NSDictionary *)_data{
    NSDictionary *callBackRes = @{};

    if (![Util notNull:_data k:@"data"]) return callBackRes;
    NSDictionary *data = _data[@"data"];

    NSString *userId = nil;
    if ([Util notNull:data k:@"user_id"]) userId = data[@"user_id"];

    Database *db = [GlobalData shareInstance].db;
    NSArray *queryRes = [db device_query:userId];

    NSDictionary *res = @{@"devices": queryRes};
    callBackRes = [Util callBackJson:res];
    return callBackRes;
}

@end